package com.ulic.customerManApplication.documentmodule.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;

import com.ulic.customerManApplication.base.BaseDao;
import com.ulic.customerManApplication.constant.CONSTANT;
import com.ulic.customerManApplication.documentmodule.bo.Document;
import com.ulic.customerManApplication.documentmodule.dao.DocumentDao;
import com.ulic.customerManApplication.documentmodule.vo.DocumentVO;
import com.ulic.customerManApplication.pager.PagerModel;

@Component("documentDao")
public class DocumentDaoImpl extends BaseDao implements DocumentDao {

	Session session = null;
	@Override
	public Document getDocument(int id) {
		// TODO Auto-generated method stub
		session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		Criteria cri = session.createCriteria(Document.class);
		List list = cri.add(Restrictions.eq("id", id)).list();
		Document document = null;
		if(list.size()>0){
			document = (Document)list.get(0);
		}
		return document;
	}

	@Override
	public PagerModel getDocuments(int curPage) {
		// TODO Auto-generated method stub
		String sql = " from Document document order by uploadDate desc";
		return searchPaginated(sql, curPage, CONSTANT.PageSize);
	}

	@Override
	public PagerModel getDocuments(DocumentVO documentVO, int curPage) {
		// TODO Auto-generated method stub
		StringBuilder sb = new StringBuilder();
		 
		sb.append(" from Document document where 1=1 ");
		if(documentVO != null){
			if(documentVO.getStartDate() != null && documentVO.getStartDate().length() != 0){
				sb.append(" and to_date(document.uploadDate,'YYYY-MM-DD  HH24:MI:SS')>= to_date('"+documentVO.getStartDate()+" 00:00:00"+"','YYYY-MM-DD  HH24:MI:SS') ");
			}
			if(documentVO.getEndDate() != null && documentVO.getEndDate().length() != 0){
				sb.append(" and to_date(document.uploadDate,'YYYY-MM-DD  HH24:MI:SS') <= to_date('"+documentVO.getEndDate()+" 23:59:59"+"','YYYY-MM-DD  HH24:MI:SS') ");
			}
			if(documentVO.getDocument() != null){
				Document document = documentVO.getDocument();
				if(document.getTitle()!= null && document.getTitle().length()!= 0){
					sb.append(" and document.title like '%"+document.getTitle().trim()+"%'");
				}
			}
		}
		sb.append(" order by uploadDate desc");
		return searchPaginated(sb.toString(), curPage, CONSTANT.PageSize);
	}

	@Override
	public void insertDocument(Document document) throws Exception {
		// TODO Auto-generated method stub
		session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		session.save(document);
		session.flush();
	}

	@Override
	public void deleteDocument(Document document) {
		// TODO Auto-generated method stub
		session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		session.delete(document);
		session.flush();
	}

}
